package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Issue;
import com.cskaoyan.bean.domain.IssueExample;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper mapper;

    @Override
    public BaseData getList(BaseParam param, String question) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        IssueExample example = new IssueExample();

        if (question != null&&!question.trim().equals("")) {
            example.createCriteria().andQuestionLike("%" + question.trim() + "%");
        }

        example.createCriteria().andDeletedEqualTo(false);
        if (param.getSort() != null && param.getOrder() != null) {
            example.setOrderByClause(param.getSort() + " " + param.getOrder());
        }
        List<Issue> issueList = mapper.selectByExample(example);

        PageInfo<Issue> pageInfo = new PageInfo<>(issueList);
        Integer total = MyUtils.getInteger(pageInfo.getTotal());
        return new BaseData(pageInfo.getPageNum(), pageInfo.getSize(), total, pageInfo.getPages(), issueList);

    }

    @Override
    public Issue createIssue(Issue issue) {
        Date date = new Date();

        issue.setUpdateTime(date);
        issue.setAddTime(date);

        // useGeneratedKeys="true" keyProperty="id" keyColumn="id"
        mapper.insertSelective(issue);

        return issue;
    }

    @Override
    public void deleteIssue(Issue issue) {
        issue.setDeleted(true);
        mapper.updateByPrimaryKeySelective(issue);
    }

    @Override
    public Issue updateIssue(Issue issue) {
        Date date = new Date();

        issue.setUpdateTime(date);

        mapper.updateByPrimaryKeySelective(issue);

        return issue;
    }


}
